package site.marqstree.latte.ec.main.personal.profile;

import android.content.DialogInterface;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import site.marqstree.latte.core.delegates.LatteDelegate;
import site.marqstree.latte.core.net.RestClient;
import site.marqstree.latte.core.net.callback.ISuccess;
import site.marqstree.latte.core.util.callback.CallbackManager;
import site.marqstree.latte.core.util.callback.CallbackType;
import site.marqstree.latte.core.util.callback.IGlobalCallback;
import site.marqstree.latte.core.util.log.LatteLogger;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.main.personal.list.ListBean;
import site.marqstree.latte.ec.profile.AppProfile;
import site.marqstree.latte.ui.date.DateDialogUtil;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.personal.profile
 * 文件名: UserProfileItemClickListener
 * 创建者: marqstree
 * 创建时间: 2020/2/4 9:31
 * 描述: TODO
 */
public class UserProfileItemClickListener implements OnItemClickListener {

    private final UserProfileDelegate DELEGATE;

    private String[] mGenders = new String[]{"男", "女", "保密"};

    public UserProfileItemClickListener(UserProfileDelegate DELEGATE) {
        this.DELEGATE = DELEGATE;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        final ListBean bean = (ListBean) adapter.getData().get(position);
        final int id = bean.getId();
        switch (id) {
            case 1:
                //开始照相机或选择图片
                CallbackManager.getInstance()
                        .addCallback(CallbackType.ON_CROP, new IGlobalCallback<Uri>() {
                                    //args:剪切后的图片路径的uri
                                    @Override
                                    public void executeCallback(@Nullable Uri args) {
                                        LatteLogger.i("拍照","剪切回调");
                                        //载入图片,刷新UI
                                        final ImageView avatar = (ImageView) view.findViewById(R.id.img_arrow_avatar);
                                        Glide.with(DELEGATE)
                                                .load(args)
                                                .into(avatar);

                                        //将头像图片上传给后台服务器
                                        //RestClient.builder()
                                        //        .url(AppProfile.UPLOAD_IMG_URL)
                                        //        .loader(DELEGATE.getContext())
                                        //        .file(args.getPath())
                                        //        .success(new ISuccess() {
                                        //            @Override
                                        //            public void onSuccess(String response) {
                                        //                LatteLogger.d("ON_CROP_UPLOAD", response);
                                        //                final String path = JSON.parseObject(response).getJSONObject("result")
                                        //                        .getString("path");
                                        //
                                        //                //用户更新信息
                                        //                RestClient.builder()
                                        //                        .url(AppProfile.UPDATE_USER_PROFILE_URL)
                                        //                        .params("avatar", path)
                                        //                        .loader(DELEGATE.getContext())
                                        //                        .success(new ISuccess() {
                                        //                            @Override
                                        //                            public void onSuccess(String response) {
                                        //                                //获取更新后的用户信息，然后更新本地数据库
                                        //                                //没有本地数据的APP，每次打开APP都请求API，获取信息
                                        //                            }
                                        //                        })
                                        //                        .build()
                                        //                        .post();
                                        //            }
                                        //        })
                                        //        .build()
                                        //        .upload();
                                    }
                                }
                        );
                DELEGATE.startCameraWithCheck();
                break;
            case 2:
                final LatteDelegate nameDelegate = bean.getDelegate();
                DELEGATE.getSupportDelegate().start(nameDelegate);
                break;
            case 3:
                getGenderDialog(view);
                break;
            case 4:
                final DateDialogUtil dateDialogUtil = new DateDialogUtil();
                dateDialogUtil.setDateListener(new DateDialogUtil.IDateListener() {
                    @Override
                    public void onDateChange(String date) {
                        final TextView textView = (TextView) view.findViewById(R.id.tv_arrow_value);
                        textView.setText(date);
                    }
                });
                dateDialogUtil.showDialog(DELEGATE.getContext());
                break;
            default:
                break;
        }
    }

    private void getGenderDialog(View view) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(DELEGATE.getContext());
        builder.setSingleChoiceItems(mGenders, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                final TextView textView = (TextView) view.findViewById(R.id.tv_arrow_value);
                textView.setText(mGenders[which]);
                dialog.cancel();
            }
        });
        builder.show();
    }
}
